/*
 * tools.js
 */
if (typeof ToolKit == "undefined") {
	var ToolKit = {};

	ToolKit.parseURLParameters = function(url, to, types) {
		to = to || {};
		types = types || {};

		if (typeof url == "undefined") {
			url = location.href;
		}

		var pos = url.indexOf("#");
		if (pos >= 0) {
			to["refAnchor"] = url.substring(pos + 1, url.length);
		}

		var q = url.indexOf("?");
		if (q < 0) {
			return to;
		}

		url = (url + "#").slice(q + 1, url.indexOf("#")); // toss the URL
		// fragment

		var params = url.split("&"), param, parsed = {};
		var decode = unescape;
		for (var i = 0; param = params[i]; i++) {
			var eq = param.indexOf("=");
			var name = decode(param.slice(0, eq));
			var old = parsed[name];
			if (typeof old == "undefined") {
				old = [];
			} else if (!(old instanceof Array)) {
				old = [old];
			}
			parsed[name] = old.concat(decode(param.slice(eq + 1)));
		}
		for (var i in parsed) {
			if (!parsed.hasOwnProperty(i))
				continue;
			var type = types[i] || String;
			var data = parsed[i];
			if (!(data instanceof Array)) {
				data = [data];
			}
			if (type === Boolean && data[0] == "false") {
				to[i] = false; // because Boolean("false") === true
			} else {
				to[i] = type.apply(this, data);
			}
		}
		return to;
	};

	ToolKit.formatDate = function(date, format) {
		var array = {
			"M+" : date.getMonth() + 1,
			"d+" : date.getDate(),
			"h+" : date.getHours(),
			"m+" : date.getMinutes(),
			"s+" : date.getSeconds(),
			"q+" : Math.floor((date.getMonth() + 3) / 3),
			"S" : date.getMilliseconds()
		}
		if (/(y+)/.test(format))
			format = format.replace(RegExp.$1, (date.getFullYear() + "")
					.substr(4 - RegExp.$1.length));
		for (var i in array)
			if (new RegExp("(" + i + ")").test(format))
				format = format.replace(RegExp.$1, RegExp.$1.length == 1
						? o[i]
						: ("00" + array[i]).substr(("" + array[i]).length));
		return format;
	}
}